import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hello_flutter/bloc_demo/weather/settings_bloc/index.dart';
import 'package:hello_flutter/bloc_demo/weather/theme_bloc/index.dart';
import 'package:hello_flutter/bloc_demo/weather/weather_bloc/index.dart';
import 'ui/weather_page.dart';
import 'weather_api.dart';
import 'weather_repository.dart';
import 'package:http/http.dart' as http;

///  build a Weather app in Flutter which demonstrates how to manage multiple blocs to implement dynamic theming, pull-to-refresh, and much more.
///  Our App widget can then use BlocBuilder to react to changes in ThemeState.
///
class MyApp extends StatelessWidget {
  final WeatherRepository weatherRepository = WeatherRepository(
    weatherApiClient: WeatherApiClient(
      httpClient: http.Client(),
    ),
  );

  @override
  Widget build(BuildContext context) {
    /// use MultiBlocProvider : ThemeBloc, SettingsBloc
    return MultiBlocProvider(
        providers: [
          BlocProvider<ThemeBloc>(
            create: (context) => ThemeBloc(),
          ),
          BlocProvider<SettingsBloc>(
            create: (context) => SettingsBloc(),
          ),
        ],

        child: BlocBuilder<ThemeBloc, ThemeState>(
            builder: (context, themeState) {
          return MaterialApp(
            title: 'Flutter Weather',
            theme: themeState.theme,
            home: BlocProvider(
              create: (context) => WeatherBloc(weatherRepository: weatherRepository),
              child: WeatherPage(),
            ),
          );
        }));
  }
}
